﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cvk.FAQs
{
    public class FAQBll
    {
        public static class claFAQs
        {
            public static int Add(Cvk_FQA info, string strConnect)
            {
                int id = 0;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        db.Cvk_FQAs.InsertOnSubmit(info);
                        db.SubmitChanges();
                        id = info.FQAId;
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return id;
            }
            public static bool Delete(int id, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_FQA o = db.Cvk_FQAs.Single(p => p.FQAId == id);
                        if (o != null)
                        {
                            db.Cvk_FQAs.DeleteOnSubmit(o);
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static bool UpdateIsActive(int id, bool isActive, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_FQA o = db.Cvk_FQAs.Single(p => p.FQAId == id);
                        if (o != null)
                        {
                            o.IsActive = isActive;
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static bool UpdateIsFrequency(int id, bool isFre, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_FQA o = db.Cvk_FQAs.Single(p => p.FQAId == id);
                        if (o != null)
                        {
                            o.IsFrequency = isFre;
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static bool Update(Cvk_FQA o, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_FQA ol = db.Cvk_FQAs.Single(p => p.FQAId == o.FQAId);
                        if (o != null)
                        {
                            ol.Answer = o.Answer;
                            ol.AnswerDate = o.AnswerDate;
                            ol.IPAddress = o.IPAddress;
                            ol.IsActive = o.IsActive;
                            ol.IsFrequency = o.IsFrequency;
                            ol.ProvinceId = o.ProvinceId;
                            ol.WardId = o.WardId;
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static Cvk_FQA GetFQAById(int id, string strConnect)
            {
                Cvk_FQA o = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        o = db.Cvk_FQAs.Single(p => p.FQAId == id);
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return o;
            }
            public static List<Cvk_FQA> GetFQAByCategoryId(int categoryId, string strConnect)
            {
                List<Cvk_FQA> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_FQAs
                                where p.FQACategoryId == categoryId 
                                orderby p.CreateDate descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static List<Cvk_FQA> GetFQAByCategoryId(int categoryId, bool isActive, string strConnect)
            {
                List<Cvk_FQA> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_FQAs
                                where p.FQACategoryId == categoryId && p.IsActive==isActive
                                orderby p.CreateDate descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static List<Cvk_FQA> GetAllList(string strConnect, bool isActive)
            {
                List<Cvk_FQA> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_FQAs
                                where p.IsActive == isActive
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static List<Cvk_FQA> GetAllList(string strConnect)
            {
                List<Cvk_FQA> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_FQAs
                                orderby p.CreateDate descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }

            public static List<Cvk_FQA> GetList(int intTop, bool isActive, string strConnect)
            {
                List<Cvk_FQA> list = new List<Cvk_FQA>();
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var varList = from p in db.Cvk_FQAs
                                      where p.IsActive == isActive
                                      orderby p.AnswerDate descending
                                      select p;
                        list = varList.Take(intTop).ToList();
                    }
                    catch { list = null; }
                    finally { db.Dispose(); }
                }
                return list;
            }

            public static List<Cvk_FQA> GetList(int CategoryId, int intSkip, int intTake, bool isActive, string strConnect)
            {
                List<Cvk_FQA> list = new List<Cvk_FQA>();
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var varList = from p in db.Cvk_FQAs
                                      where p.FQACategoryId == CategoryId && p.IsActive == isActive && p.Answer != ""
                                      orderby p.AnswerDate descending
                                      select p;
                        list = varList.Skip(intSkip).Take(intTake).ToList();
                    }
                    catch { list = null; }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static int GetListCount(int CategoryId, bool isActive, string strConnect)
            {
                int result = 0;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var varList = from p in db.Cvk_FQAs
                                      where p.FQACategoryId == CategoryId && p.IsActive == isActive && p.Answer != ""
                                      orderby p.AnswerDate descending
                                      select p;
                        result = varList.Count();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return result;
            }
        }
        public static class claFAQCategory
        {
            public static int Add(Cvk_FQACategory info, string strConnect)
            {
                int id = 0;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        db.Cvk_FQACategories.InsertOnSubmit(info);
                        db.SubmitChanges();
                        id = info.FQACategoryId;
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return id;
            }
            public static List<Cvk_FQACategory> GetAllList(string strConnect, bool active)
            {
                List<Cvk_FQACategory> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_FQACategories
                                where p.IsActive == active
                                orderby p.Name descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static Cvk_FQACategory GetFQAById(string strConnect, int id)
            {
                Cvk_FQACategory o = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        o = db.Cvk_FQACategories.Single(p => p.FQACategoryId == id);
                        db.SubmitChanges();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return o;
            }
            public static string GetNameById(string strConnect, int id)
            {
                string result = string.Empty;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        result = db.Cvk_FQACategories.Single(p => p.FQACategoryId == id).Name;
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return result;
            }
        }
        public static class claReply
        {
            public static int Add(Cvk_Reply info, string strConnect)
            {
                int id = 0;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        db.Cvk_Replies.InsertOnSubmit(info);
                        db.SubmitChanges();
                        id = info.ReplyId;
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return id;
            }
            public static bool Delete(int id, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_Reply o = db.Cvk_Replies.Single(p => p.ReplyId == id);
                        if (o != null)
                        {
                            db.Cvk_Replies.DeleteOnSubmit(o);
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static bool UpdateIsActive(int id, bool isActive, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_Reply o = db.Cvk_Replies.Single(p => p.ReplyId == id);
                        if (o != null)
                        {
                            o.IsActive = isActive;
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }

            public static bool Update(Cvk_Reply o, string strConnect)
            {
                bool flag = false;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        Cvk_Reply ol = db.Cvk_Replies.Single(p => p.ReplyId == o.ReplyId);
                        if (o != null)
                        {
                            ol.Answer = o.Answer;
                            ol.IPAddress = o.IPAddress;
                            ol.IsActive = o.IsActive;
                            db.SubmitChanges();
                            flag = true;
                        }
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return flag;
            }
            public static Cvk_Reply GetReplyById(int id, string strConnect)
            {
                Cvk_Reply o = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        o = db.Cvk_Replies.Single(p => p.ReplyId == id);
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return o;
            }
            public static List<Cvk_Reply> GetReplyByFQAId(int FQAId, string strConnect)
            {
                List<Cvk_Reply> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_Replies
                                where p.FQAId == FQAId 
                                orderby p.CreateDate descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static List<Cvk_Reply> GetAllList(string strConnect, bool isActive)
            {
                List<Cvk_Reply> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_Replies
                                where p.IsActive == isActive
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }
            public static List<Cvk_Reply> GetAllList(string strConnect)
            {
                List<Cvk_Reply> list = null;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var o = from p in db.Cvk_Replies
                                orderby p.CreateDate descending
                                select p;
                        list = o.ToList();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return list;
            }


           
            public static int GetListCount(int FQAId, bool isActive, string strConnect)
            {
                int result = 0;
                using (FAQsLinqDataContext db = new FAQsLinqDataContext(strConnect))
                {
                    try
                    {
                        var varList = from p in db.Cvk_Replies
                                      where p.FQAId == FQAId && p.IsActive == isActive 
                                      orderby p.CreateDate descending
                                      select p;
                        result = varList.Count();
                    }
                    catch { }
                    finally { db.Dispose(); }
                }
                return result;
            }
        }
    }
}
